<?php

class __admin_system_images_profiles extends Module {
	
	public function __construct () {
		parent::__construct('system');
	}
	
	public function images_profiles () {
		$oResponse = $this->getImagesProfiles ();
		$oResponse->setTemplate ('system/imagesProfiles/list');
		
		if (getRequest ('created','bool')) {
			$oResponse->addObject (new SystemMessage (lang('admin_image_created','system')));
		}
		if (getRequest ('saved','bool')) {
			$oResponse->addObject (new SystemMessage (lang('admin_image_saved','system')));
		}
		if (getRequest ('deleted','bool')) {
			$oResponse->addObject (new SystemMessage (lang('admin_image_deleted','system')));
		}
		
		return $oResponse;
	}
	
	public function add_images_profile () {
		return $this->imageProfileForm ();
	}
	
	public function edit_images_profile () {
		return $this->imageProfileForm (getRequest ('param0','int'));
	}
	
	public function delete_images_profile () {
		$this->expectSecureGET ();
		
		if (!$object = ObjectsController::getInstance()->getObject (TypesController::getInstance()->getTypeByName ('image_profile'), getRequest('param0','int'))) {
			throw new NotFound;
		}
		
		$object->delete ();
		
		redirect (HTTP_HOST . '/admin/system/images_profiles?deleted');
	}
	
	public function getImagesProfiles () {
		$oTypeProf = TypesController::getInstance()->getTypeByName ('image_profile');
		
		$sel = new Selection;
		$sel->addType ($oTypeProf);
		$sel->setOrder ('title');
		
		$aObjects = ObjectsController::getInstance()->getObjectsList ($sel->getList ());
		
		$response = new ModuleResponse;
		
		foreach ($aObjects as $object) {
			$response->addObject ($object);
		}
		
		return $response;
	}
	
	private function imageProfileForm ($id = 'new') {
		$oTypeProf = TypesController::getInstance()->getTypeByName ('image_profile');
		
		if ($id != 'new') {
			if (!$object = ObjectsController::getInstance()->getObject ($oTypeProf, $id)) {
				throw new NotFound;
			}
		}
		
		$form = new Form ($oTypeProf, $id);
		$form->buildFromType ();
		
		$aErrors = array ();
		$aValues = $id == 'new' ? array () : $object->getValues();
		
		if ((getRequest ('param0') == 'save' && $id == 'new') || getRequest ('param1') == 'save') {
			$aValues = $this->getRequestData ($id, $oTypeProf);
			
			if ($id == 'new') {
				$object = new Object ($oTypeProf);
				$object->setValues ($aValues);
				
				$aErrors = $this->getValidateErrors ($object);
				
				if (!in_array ($object->getValue ('exit_type'), array('jpg','gif','png'))) {
					$aErrors['exit_type'] = lang('error_admin_image_undefined_type','system');
				}
				
				if (!count ($aErrors)) {
					$object->createNew ();
					
					redirect (HTTP_HOST . '/admin/system/images_profiles?created');
				}
			} else {
				$object->setValues ($aValues);
							
				$aErrors = $this->getValidateErrors ($object);
				
				if (!in_array ($object->getValue ('exit_type'), array('jpg','gif','png'))) {
					$aErrors['exit_type'] = lang('error_admin_image_undefined_type','system');
				}
				
				if (!count ($aErrors)) {
					$object->save ();
					
					redirect (HTTP_HOST . '/admin/system/images_profiles?saved');
				}
			}
		}
		
		$form->setErrors ($aErrors);
		$form->setValues ($aValues);
		
		$response = new ModuleResponse;
		$response->setTemplate ('system/imagesProfiles/form');
		$response->addObject ($form);
		
		return $response;
	}
	
}